package com.fizz.config;

import com.fizz.common.DataSourceTypeEnum;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;

@Aspect
@EnableAspectJAutoProxy(exposeProxy=true,proxyTargetClass=true)
@Component
public class DataSourceAopAspect implements PriorityOrdered {

    @Before("execution(* com.fizz.service..*.*(..)) "
            + " && @annotation(com.fizz.config.ReadDataSource) ")
    public void setReadDataSourceType() {
        //如果已经开启写事务了，那之后的所有读都从写库读
        DatasourceContextHolder.setRead();

    }
    @Before("execution(* com.fizz.service..*.*(..)) "
            + " && @annotation(com.fizz.config.WriteDataSource) ")
    public void setWriteDataSourceType() {
        DatasourceContextHolder.setWrite();
    }

    public int getOrder() {
        return 0;
    }
}
